package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.OrderdetailetDao;
import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Orderdetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrdersDaoImpl implements OrdersDao {
    /**
     * 功能：根据用户编号、商家编号、订单总金额、送货地址编号向订单表中添加一条记录，
     * ​ 并获取自动生成的订单编号，
     * ​ 然后根据用户编号、商家编号从购物车表中查询所有数据，批量添加到订单明细表中，
     * ​ 然后根据用户编号、商家编号删除购物车表中的数据。
     */
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;



    @Override
    public int createOrders(String userId, Integer businessId, int daId, double orderTotal) throws Exception {
        int orderId =0;
        Integer num=0;
        String sql = "insert into orders (userId,businessId,orderDate,daId,orderTotal) values (?,?,now(),?,?)";
        try {
            //Connection从ThreadLocal中获取
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            //将?替换具体的值
            pst.setString(1,userId);
            pst.setInt(2,businessId);
            pst.setInt(3,daId);
            pst.setDouble(4,orderTotal);
            //调用方法
            num= pst.executeUpdate();
            rs= pst.getGeneratedKeys();
            if (rs.next()){
                orderId= rs.getInt(1);
            }
        }finally {
            DBUtil.close(rs,pst);
        }
        return orderId;
    }


    /**
     * 功能：根据订单编号查询订单信息，包括所属商家信息，和此订单的所有订单明细信息
     * @param orderId
     * @return
     * @throws Exception
     */
    @Override
    public Orders getOrdersById(Integer orderId) throws Exception {
        Orders orders=new Orders();
        //sql查询订单和商家的关联
        String sql="SELECT o.*,b.* FROM orders o,business b WHERE o.businessId=b.businessId AND o.orderId=?";
        try {
            //Connection从ThreadLocal中获取
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            //将?替换具体的值
            pst.setInt(1,orderId);
            //查询
            rs= pst.executeQuery();
            //遍历
            while (rs.next()){

                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));
                Business business=new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));

                orders.setBusiness(business);
            }
        }finally {
            DBUtil.close(rs,pst);
        }
        return orders;
    }

    /**
     * 功能：根据用户编号查询此用户的所有订单信息
     * @param userId
     * @return
     * @throws Exception
     */

    @Override
    public List<Orders> listOrdersByUserId(String userId) throws Exception {
        List<Orders> list = new ArrayList<>();
        //sql查询订单和商家的关联
        String sql="SELECT o.*,b.* FROM orders o,business b WHERE o.businessId=b.businessId AND userId=?";
        try {
            //Connection从ThreadLocal中获取
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            //将?替换具体的值
            pst.setString(1,userId);
            rs= pst.executeQuery();
            while (rs.next()){
                Orders orders=new Orders();
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));
                Business business=new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));

                //调用OrderDetailetDaoImpl查询明细和食品的方法获得Detailet
               OrderdetailetDao orderDetailetDao=new OrderdetailetDaoImpl();
                List<Orderdetailet>list1= orderDetailetDao.listOrderDetailetByOrderId(rs.getInt("orderId"));
                //存入orders对象中
                orders.setOrderdetailetList(list1);
                orders.setBusiness(business);
                list.add(orders);
            }
        }finally {
            DBUtil.close(rs,pst);
        }
        return list;
    }


}

